package com.kungfuhacking.wristbandpro.base.view;

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.kungfuhacking.wristbandpro.R;
import com.kungfuhacking.wristbandpro.base.Mapplication;
import com.kungfuhacking.wristbandpro.base.presenter.IHomePresenter;
import com.kungfuhacking.wristbandpro.chat.FragmentChat;
import com.kungfuhacking.wristbandpro.find.FragmentFind;
import com.kungfuhacking.wristbandpro.location.FragmentOne;
import com.kungfuhacking.wristbandpro.my.FragmentMy;
import com.kungfuhacking.wristbandpro.utils.Constant;
import com.kungfuhacking.wristbandpro.utils.DensityUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import mqttresource.MqttException;


/**
 * 主页
 * Created by Aaron on 17/3/30.
 */

public class HomeActivity extends BaseActivity implements View.OnClickListener, IHomeView {

    public Mapplication mApplication;

    private ImageView ivone;
    private TextView tvone;
    private RelativeLayout rlone;
    private ImageView ivtwo;
    private TextView tvtwo;
    private RelativeLayout rltwo;
    private ImageView ivthree;
    private TextView tvthree;
    private RelativeLayout rlthree;
    private ImageView ivfour;
    private TextView tvfour;
    private RelativeLayout rlfour;

    private FragmentManager fragmentManager;

    private FragmentOne fragmentOne;
    private FragmentChat fragmentTwo;
    private FragmentMy fragmentFour;
    private FragmentFind fragmentFind;

    private IHomePresenter iHomePresenter = new IHomePresenter(this);
    private TextView tv_msg_count;
    private String TAG = getClass().getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_home);

//        getWindow().setFormat(PixelFormat.TRANSLUCENT);

        mApplication = (Mapplication) getApplication();
        mApplication.loginMqtt();

        initBottomView();
        initFragment();
        //设置极光推送的tag
        iHomePresenter.setAlias(sp.getString(Constant.MOBILE, ""));

        EventBus.getDefault().register(this);

    }

    //接受到新消息
    @Subscribe(threadMode = ThreadMode.MAIN) //在ui线程执行
    public void onDataSynEvent(String mb) {
//        Log.d(TAG, " === 收到 消息  ?  ===== ");
        iHomePresenter.handleMessage(mb);
    }

    @Override
    protected void onResume() {
        super.onResume();
        iHomePresenter.setRedCount();
    }

    /**
     * 加载底部按钮
     */
    private void initBottomView() {

        tv_msg_count = (TextView) findViewById(R.id.tv_msg_count);

        this.rlone = (RelativeLayout) findViewById(R.id.rl_one);
        this.tvone = (TextView) findViewById(R.id.tv_one);
        this.ivone = (ImageView) findViewById(R.id.iv_one);

        this.rltwo = (RelativeLayout) findViewById(R.id.rl_two);
        this.tvtwo = (TextView) findViewById(R.id.tv_two);
        this.ivtwo = (ImageView) findViewById(R.id.iv_two);

        this.rlthree = (RelativeLayout) findViewById(R.id.rl_three);
        this.tvthree = (TextView) findViewById(R.id.tv_three);
        this.ivthree = (ImageView) findViewById(R.id.iv_three);

        this.rlfour = (RelativeLayout) findViewById(R.id.rl_four);
        this.tvfour = (TextView) findViewById(R.id.tv_four);
        this.ivfour = (ImageView) findViewById(R.id.iv_four);

        rlone.setOnClickListener(this);
        rltwo.setOnClickListener(this);
        rlthree.setOnClickListener(this);
        rlfour.setOnClickListener(this);

    }

    private void initFragment() {

        fragmentManager = getFragmentManager();

        ivone.setSelected(true);
        tvone.setTextColor(getResources().getColor(android.R.color.holo_red_light));

        chooseFragment(0);

    }

    /**
     * 选择对应的fragment
     *
     * @param index 对应的fragment的角标
     */
    private void chooseFragment(int index) {

        FragmentTransaction transaction = null;
        if (fragmentManager != null)
            transaction = fragmentManager.beginTransaction();

        // 先移除掉所有的Fragment，以防止有多个Fragment显示在界面上的情况
//        removeFragments(transaction);

        if (transaction == null)
            return;

        switch (index) {

            case 0: //

                if (fragmentOne == null)
                    fragmentOne = new FragmentOne();

                if (!fragmentOne.isAdded())
                    transaction.replace(R.id.fl_base, fragmentOne, "ZERO");
                else
                    fragmentOne.reBottonLocation();

                break;

            case 1: //

                if (fragmentTwo == null)
                    fragmentTwo = new FragmentChat();

                if (!fragmentTwo.isAdded())
                    transaction.replace(R.id.fl_base, fragmentTwo, "ONE");

                break;

            case 2:

                if (fragmentFind == null)
                    fragmentFind = new FragmentFind();

                if (!fragmentFind.isAdded())
                    transaction.replace(R.id.fl_base, fragmentFind, "TWO");

                break;


            case 3://

                if (fragmentFour == null)
                    fragmentFour = new FragmentMy();

                if (!fragmentFour.isAdded())
                    transaction.replace(R.id.fl_base, fragmentFour, "THREE");

                break;
        }

        transaction.commit();

    }

    /**
     * 把所有的图标都变灰
     * 字体还原
     */
    private void setSelectFalse() {

        ivone.setSelected(false);
        ivtwo.setSelected(false);
        ivthree.setSelected(false);
        ivfour.setSelected(false);

        tvone.setTextColor(getResources().getColor(R.color.text_color));
        tvtwo.setTextColor(getResources().getColor(R.color.text_color));
        tvthree.setTextColor(getResources().getColor(R.color.text_color));
        tvfour.setTextColor(getResources().getColor(R.color.text_color));

    }

    @Override
    public void setUnredCount(String unread) {
        tv_msg_count.setText(unread);
    }


    @Override
    public void setRedVisible(boolean visible) {
        tv_msg_count.setVisibility(visible ? View.VISIBLE : View.INVISIBLE);
    }

    @Override
    public void setUnredBackground(boolean isCircle) {

        if (isCircle)
            tv_msg_count.setPadding(0, 0, 0, DensityUtil.dip2px(this, 3));
        else
            tv_msg_count.setPadding(DensityUtil.dip2px(this, 4), 0, DensityUtil.dip2px(this, 4), DensityUtil.dip2px(this, 3));

        tv_msg_count.setBackgroundResource(isCircle ? R.drawable.shape_circle_red : R.drawable.shape_oval_rectangle_red);
    }


    @Override
    public void onClick(View v) {

        switch (v.getId()) {

            case R.id.rl_one://第一个

                chooseFragment(0);

                //把所有的图标都变灰
                setSelectFalse();

                ivone.setSelected(true);
                tvone.setTextColor(getResources().getColor(android.R.color.holo_red_light));

                break;

            case R.id.rl_two://第二个

                chooseFragment(1);

                //把所有的图标都变灰
                setSelectFalse();

                ivtwo.setSelected(true);
                tvtwo.setTextColor(getResources().getColor(android.R.color.holo_red_light));

                break;

            case R.id.rl_three://第三个

                chooseFragment(2);

                //把所有的图标都变灰
                setSelectFalse();

                ivthree.setSelected(true);
                tvthree.setTextColor(getResources().getColor(android.R.color.holo_red_light));

                break;

            case R.id.rl_four://第四个

                chooseFragment(3);

                //把所有的图标都变灰
                setSelectFalse();

                ivfour.setSelected(true);
                tvfour.setTextColor(getResources().getColor(android.R.color.holo_red_light));

                break;

        }

    }

    @Override
    public void onBackPressed() {//退出应用
        iHomePresenter.exitApp();
    }

    @Override
    public Context getContex() {
        return getApplication();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        EventBus.getDefault().unregister(this);
        iHomePresenter.clearRequest();

    }

    @Override
    public void finishActivity() {
        try {
            mApplication.exitMqtt();
        } catch (MqttException e) {
            e.printStackTrace();
        }
        finish();
    }


}
